<?php


namespace Biz\service\Store;


use Biz\model\store\VirtualPhoneRecord;
use Biz\model\store\VirtualPhoneUseRecord;
use Doraemon\tools\Log;

class VirtualPhoneCallRecordingsHandler
{
    public static function handle($arr)
    {
        //通知主键数组
        $idNotifyArr = [];
        //通知绑定关系ID数组
        $subIdNotifyArr = [];
        foreach ($arr as $nv) {
            if (!empty($nv['id']) && !empty($nv['sub_id']) && !empty($nv['pool_key'])) {
                if (VirtualPhone::POOL_KEY == $nv['pool_key']) {
                    $idNotifyArr[] = $nv['id'];
                    $subIdNotifyArr[] = $nv['sub_id'];
                }
            }
        }
        //避免非法访问， 带来垃圾数据
        if (empty($idNotifyArr)) {
            return false;
        }
        //查找业务id， 避免垃圾数据
        $subIdInfoArr = VirtualPhoneRecord::getInfoBySubid($subIdNotifyArr);
        if (empty($subIdInfoArr)) {
            return false;
        }

        //业务数据转map
        $subIdInfoMap = [];
        foreach ($subIdInfoArr as $v) {
            $subIdInfoMap[$v['subs_id']] = $v;
        }

        //查找是否保存， 避免幂等请求
        $idInfoArr = VirtualPhoneUseRecord::getInfoByReportid($idNotifyArr);
        //保存数据转map
        $idInfoMap = [];
        foreach ($idInfoArr as $v) {
            $idInfoMap[$v['report_id']] = $v['id'];
        }

        //拼接保存数据--批量保存
        $saveDataArr = [];
        //打入日志--业务关系
        $subIdExist = [];
        //重复请求
        $repeatId = [];
        foreach ($arr as $nv) {
            if (empty($nv['id']) || empty($nv['sub_id'])) {
                continue;
            }
            //不存在业务关系
            if (!isset($subIdInfoMap[$nv['sub_id']])) {
                $subIdExist[] = $nv['id'];
                continue;
            }
            //已经保存过
            if (isset($idInfoMap[$nv['id']])) {
                $repeatId[] = $nv['id'];
                continue;
            }
            //内部去重
            $idInfoMap[$nv['id']] = 1;
            $subInfo = $subIdInfoMap[$nv['sub_id']];

            $saveDataArr[] = [
                'user_uid' => $subInfo['user_uid'],
                //用户uid
                'store_id' => $subInfo['store_id'],
                //门店id
                'subs_id' => $subInfo['subs_id'],
                //poolkey
                'pool_key' => $nv['pool_key'],
                //阿里云的绑定关系id
                'report_id' => $nv['id'],
                //报告消息的主键id
                'call_time' => strtotime($nv['call_time']),
                //主叫拨打时间
                'total_time' => strtotime($nv['release_time']) - strtotime($nv['start_time']),
                //通话时间  （s）
                'call_type' => $nv['call_type'] + 1,
                //呼叫类型 1-主叫  2-被叫 3-短信发送 4-短信接收 5-呼叫拦截 6-短信收发拦截
                'unconnected_status' => isset($nv['unconnected_cause']) ? ($nv['unconnected_cause'] + 1) : 1,
                //未接通通话的原因归类 1-正常通话  2-黑名单拦截 3-黑名单拦截 4-黑名单拦截 5-其他
                'addtime' => time(),
                //添加时间
            ];
        }

        Log::info("SecretReport Callback Result", [
            "subIdExist" => $subIdExist,
            "repeatId" => $repeatId,
            "idInfoArr" => $idInfoArr,
            "arr" => $arr
        ]);
        if (!empty($saveDataArr)) {
            return VirtualPhoneUseRecord::saveAll($saveDataArr);
        }
        return 0;
    }
}